//
//  CollectionViewGalleryLayout.swift
//  Pods
//
//  Created by LC_WJH on 2017/9/16.
//
//

import UIKit

public class CollectionViewGalleryLayout:UICollectionViewFlowLayout{
    
    
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder);
        initIt();
    }
    
    public required override init() {
        super.init();
        initIt();
    }
    
    func initIt(){
        
        scrollDirection =  UICollectionView.ScrollDirection.horizontal;
  
        
        
        
    }
    
    
    override public func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        
        if collectionView == nil {
            return CGPoint.zero;
        }
        
        
        //proposedContentOffset是没有设置对齐时本应该停下的位置（collectionView落在屏幕左上角的点坐标）
        
        
        let horizontalCenter:CGFloat = proposedContentOffset.x + collectionView!.bounds.width / 2.0;
        let targetRect:CGRect = CGRect.init(x: proposedContentOffset.x, y:0.0, width: collectionView!.bounds.size.width, height: collectionView!.bounds.size.height)
        
        
        //targetRect.origin = CGPoint.init(x:horizontalCenter - 2  , y: collectionView!.bounds.height / 2.0 - 2);
        //targetRect.size  = CGSize.init(width: 4, height: 4);
       
        if  let arrs:[UICollectionViewLayoutAttributes] =  layoutAttributesForElements(in: targetRect) {
            
            //对当前屏幕中的UICollectionViewLayoutAttributes逐个与屏幕中心进行比较，找出最接近中心的一个
            var adj:CGFloat = 0;
            for arr:UICollectionViewLayoutAttributes in arrs{
                
                let itemHorizontalCenter = arr.center.x;
                
                if abs( itemHorizontalCenter - horizontalCenter ) < 150 {
                    adj  = itemHorizontalCenter - horizontalCenter;
                    break;
                }
                
                
            }
            
            print("\(proposedContentOffset.debugDescription)");
            return CGPoint.init(x: proposedContentOffset.x + adj, y: proposedContentOffset.y)
        }
        
        
        
        
        
        return proposedContentOffset;
        
    }
    
    override public func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        let arrs:[UICollectionViewLayoutAttributes]? = super.layoutAttributesForElements(in: rect)
        
        if collectionView != nil && arrs != nil {
            
            let visibleRect:CGRect = CGRect.init(origin: collectionView!.contentOffset, size: collectionView!.bounds.size);
            for arr:UICollectionViewLayoutAttributes in arrs!{
                
                let distance:CGFloat = visibleRect.origin.x + visibleRect.size.width / 2  - arr.center.x;
                
                if  abs(distance) < 300 {
                    
                    let scaleFactor:CGFloat = abs(distance) / 300;
                    let zooM:CGFloat = 1.0 +  0.5 *  (1.0 - scaleFactor);
                    let alpha:CGFloat = 0.3 +  0.7 *  (1.0 - scaleFactor);
                    arr.transform3D = CATransform3DMakeScale(zooM,zooM, 1.0);
                    arr.zIndex = 1 ;
                    arr.alpha = alpha;
                }
   
            }
            
        }
        
        
        return arrs;
    }
 
    override public func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true;
    }
    
    
}
